import indexRouter from './index'
import { defineAsyncComponent } from 'vue'

const routeAllPathToCompMap =import.meta.glob(`../views/**/*.vue`);

export function loadNav() {

//获得路由数据
  let tokenData = JSON.parse(sessionStorage.getItem('loginData'));

  if (!!!tokenData) {
    return false;
  }
  let navList = JSON.parse(sessionStorage.getItem('loginData')).userMenu

  //处理动态路由
  let foo = (data) => {
    data.forEach((k,i)=>{
      if(k.menuComponent != 'RouteView' ){// RouteView  渲染main
        if(!!k.target){//有数据
          //target 打开方式
          //  _blank:外部链接   _innerblank:外部打开内部路由  外部不处理
          if(k.target == '_innerblank'){
            indexRouter.addRoute({
              path: !!k.menuUrl ? '/' + k.menuUrl : '',
              name: !!k.menuUrl ? k.menuUrl : k.id,
              meta: {
                title: k.name,
                keepAlive: false,
                menuParam: !!k.menuParam ? k.menuParam : '',
              },
              // component: () =>  import(`../views/${k.menuComponent }.vue`),//wzImport(k.menuComponent),
              // component: () => defineAsyncComponent(() => import(`../views/${k.menuComponent }.vue`) ),//wzImport(k.menuComponent),
              // component: () => defineAsyncComponent(() => import('../views/' + k.menuComponent + '.vue') ),//wzImport(k.menuComponent),
              component: routeAllPathToCompMap[`../views/${k.menuComponent }.vue`],//wzImport(k.menuComponent),
            })
          }
        }else{
          if(!k.menuHide){// true 数据看板    false 整体的
            indexRouter.addRoute('_main',{
              path: !!k.menuUrl ? '/' + k.menuUrl : '',
              name: !!k.menuUrl ? k.menuUrl : k.id,
              meta: {
                title: k.name,
                keepAlive: false,
                menuParam: !!k.menuParam ? k.menuParam : '',
              },
              // component: () => import(`../views/${k.menuComponent }.vue`),//wzImport(k.menuComponent),
              // component: () => defineAsyncComponent(() => import(`../views/${k.menuComponent }.vue`) ),//wzImport(k.menuComponent),
              // component: () => defineAsyncComponent(() => import('../views/' + k.menuComponent + '.vue') ),//wzImport(k.menuComponent),
              component: routeAllPathToCompMap[`../views/${k.menuComponent }.vue`],//wzImport(k.menuComponent),
            })
          }else{
            indexRouter.addRoute('_boardMain',{
              path: !!k.menuUrl ? '/' + k.menuUrl : '',
              name: !!k.menuUrl ? k.menuUrl : k.id,
              meta: {
                title: k.name,
                keepAlive: false,
                menuParam: !!k.menuParam ? k.menuParam : '',
              },
              // component:() => import(`../views/${k.menuComponent }.vue`),//wzImport(k.menuComponent),
              // component: () => defineAsyncComponent(() => import(`../views/${k.menuComponent }.vue`) ),//wzImport(k.menuComponent),
              // component: () => defineAsyncComponent(() => import('../views/' + k.menuComponent + '.vue') ),//wzImport(k.menuComponent),
              component: routeAllPathToCompMap[`../views/${k.menuComponent }.vue`],//wzImport(k.menuComponent),
            })
          }

        }

      }
      if (k.children.length > 0) {//有子集
        foo(k.children)
      }
    })

    // let dynamicVehicle = `dataBoard/dynamicVehicle.vue`
    //
    //
    // indexRouter.addRoute('_boardMain',{
    //   path: '/dynamicVehicle',
    //   name: 'dynamicVehicle',
    //   meta: {
    //     title: 'dynamicVehicle',
    //     keepAlive: false,
    //     menuParam: '',
    //   },
    //   component: () => import(`../views/${dynamicVehicle}`),
    //   // component: () => import('../views/dataBoard/dynamicVehicle.vue'),
    //   // component: () =>import('../views/dataBoard/'+dynamicVehicle+'.vue'),//wzImport(k.menuComponent),
    // })


  };

  foo(navList.modules[0].menus)




  //固定其它路由 追加 为了防止 刷新页面的时候 没有路由 直接跳转404  所以 统一追加
  // indexRouter.addRoute({path: '*', redirect: "/404", meta: {hideInMenu: true}})
  indexRouter.addRoute({path: '/401', name: 'error_401', meta: {hideInMenu: true},component: () => import('@/components/error-page/401.vue')})
  indexRouter.addRoute({path: '/500', name: 'error_500', meta: {hideInMenu: true}, component: () => import('@/components/error-page/500.vue')})
  indexRouter.addRoute({path: '/404', name: 'error_404', meta: {hideInMenu: true}, component: () => import('@/components/error-page/404.vue')})

}



export const navList = [
  {path: '',name: '_login',component:  () => import('../views/login/index.vue')},
  //整体的
  {path: '', name: "_main",meta: {title: '主体'},component: () => import('../views/main.vue'),children: [
      // {path: '/menuHome',name: 'menuHome',component:  () => import('../views/menuHome.vue')},
    ]
  },
  //  数据看板的
  {path: '', name: "_boardMain",meta: {title: 'boardMain'},component: () => import('../views/dataBoard/boardMain.vue'),children: []},
]



// 递归 获取 第一个菜单
export function getFirstMenu (data) {
  for (let i = 0; i < data.length; i++) {
    if(!!data[i].children && data[i].children.length > 0){
      return getFirstMenu(data[i].children)
    }else{
      return data[i].menuUrl;
    }
  }
}


